package com.zdy.worldtion.base;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;

import com.zdy.worldtion.R;
import com.zdy.worldtion.app.MyApplication;
import com.zdy.worldtion.utils.LoadingDialogManager;
import com.gyf.barlibrary.ImmersionBar;

import androidx.fragment.app.FragmentActivity;
import butterknife.ButterKnife;

/**
 * 简单类型BaseActivity
 * (1.自带公共返回事件处理的简洁基类，不适用于有回传值数据的界面)
 * Created by wxl
 */

public abstract class BaseSimpleActivity extends FragmentActivity {

    public Activity mContext = null;
    private ImmersionBar mImmersionBar;
    public LoadingDialogManager loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView();
        ButterKnife.bind(this);

        MyApplication.getInstance().addActivities(this);
        mImmersionBar = ImmersionBar.with(this).statusBarColor(R.color.white).keyboardEnable(true).
                statusBarDarkFont(true, 0.2f).fitsSystemWindows(true);  //使用该属性,必须指定状态栏颜色; //可以为任意view;
        mImmersionBar.init();
        loading = LoadingDialogManager.create(this);

        initData();
        loadData();
        showContent();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    protected void showContent() {

    }

    protected abstract void setContentView();

    protected abstract void initData();

    protected void loadData() {

    }

    public void showProgress() {
        if (loading != null) {
            loading.show();
        }
    }

    public void hideProgress() {
        if (loading != null) {
            loading.dismiss();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();  //必须调用该方法，防止内存泄漏，不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
        }
        if (loading != null) {
            loading.destroy();
        }
    }

    public ImmersionBar getmImmersionBar() {
        return mImmersionBar;
    }

    /**
     * 实现字体不随系统调节改变
     *
     * @param newConfig newConfig
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
}
